package com.sgb.kjwl.view.ui.adapter.shopping;

import android.content.Context;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.NewGoodsDetailEntity;
import com.sgb.kjwl.model.entity.SkuCheckEntity;
import com.sgb.kjwl.model.entity.SkuCombinationEntity;
import com.sgb.kjwl.model.entity.user.SkuItemAttrEntity;
import com.sgb.kjwl.view.ui.adapter.material.SkuChildAdapter;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter2;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.ArrayList;
import java.util.List;

public class SelfShopAttrAdapter extends BaseRecycleAdapter2<SkuItemAttrEntity> {
    private RecyclerView recyclerView;
    private SkuChildAdapter skuChildAdapter;
    private List<SkuCombinationEntity> list = null;
    private IPrice iPrice;


    public SelfShopAttrAdapter(Context context, List<SkuItemAttrEntity> data) {
        super(context, R.layout.item_check_sku, data);
    }

    public void setCheckData(List<SkuCombinationEntity> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public void convert(ItemViewHolder holder, SkuItemAttrEntity entity, int position) {
        holder.setBinding(BR.viewModel, entity);
        recyclerView = holder.itemView.findViewById(R.id.params_list);
        skuChildAdapter = new SkuChildAdapter(context, entity.getList());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 4);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setAdapter(skuChildAdapter);
        skuChildAdapter.setItemSelect(true);
        skuChildAdapter.setOnItemClick(new AdapterOnItemClick<SkuItemAttrEntity.Child>() {

            @Override
            public void onItemClick(SkuItemAttrEntity.Child child, int pos) {
                datas.get(position).getList().get(pos).setChecked1(true);
                iPrice.getPosition(position, pos);
            }
        });
    }

    public interface IPrice {
        void getMoney(String money);
        void getPosition(int x,int y);

    }


    public void setChoicedata(List<NewGoodsDetailEntity.commoditySkuListItem> choicedata) {

        if(datas!=null){
            for (int i = 0; i<datas.size(); i++) {
                for (int i1 = 0; i1 < datas.get(i).getList().size(); i1++) {
                    for (NewGoodsDetailEntity.commoditySkuListItem choicedatum : choicedata) {
                        if(datas.get(i).getList().get(i1).getValData().equals(choicedatum.getVal())&&choicedatum.getValId().equals(datas.get(i).getList().get(i1).getValId())){
                         datas.get(i).getList().get(i1).setChecked(true);
                         if(iPrice!=null){
                             iPrice.getPosition(i, i1);
                         }
                        }
                    }
                }
            }
        }
        notifyDataSetChanged();
    }

    public void OnPrice(IPrice iPrice) {
        this.iPrice = iPrice;
    }
}
